package Transaksi;

import Login.Koneksi;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Elisa
 */
public class Barang {

    private Connection conn = null;
    private String kodeBarang, namaBarang;
    private Double hargaJual, hargaBeli, jumlah;
    private static Barang brg;

    public Barang(Connection koneksi) {
        this.conn = koneksi;
    }

    public static Barang getBarang() {
        Koneksi k = new Koneksi();
        if (brg == null) {
            brg = new Barang(k.getConnection());
        }
        return brg;
    }

    Barang() {
    }

    public String getKodeBarang() {
        return kodeBarang;
    }

    public void setKodeBarang(String kodeBarang) {
        this.kodeBarang = kodeBarang;
    }

    public String getNamaBarang() {
        return namaBarang;
    }

    public void setNamaBarang(String namaBarang) {
        this.namaBarang = namaBarang;
    }

    public Double getHargaJual() {
        return hargaJual;
    }

    public void setHargaJual(Double hargaJual) {
        this.hargaJual = hargaJual;
    }

    public Double getHargaBeli() {
        return hargaBeli;
    }

    public void setHargaBeli(Double hargaBeli) {
        this.hargaBeli = hargaBeli;
    }

    public Double getJumlah() {
        return jumlah;
    }

    public void setJumlah(Double jumlah) {
        this.jumlah = jumlah;
    }

    public void simpanBarangPrepared(Barang bar) throws SQLException {
        PreparedStatement prepare = null;
        String kodebarang = bar.getKodeBarang();
        String namabarang = bar.getNamaBarang();
        double hargabeli = bar.getHargaBeli();
        double hargajual = bar.getHargaJual();
        double jumlah = bar.getJumlah();

        String sql = "call tambahBarang (?,?,?,?,?)";
        prepare = conn.prepareStatement(sql);
        prepare.setString(1, kodebarang);
        prepare.setString(2, namabarang);
        prepare.setDouble(3, hargabeli);
        prepare.setDouble(4, hargajual);
        prepare.setDouble(5, jumlah);

        prepare.executeUpdate();

    }

    public void ubahBarang(Barang bar) throws SQLException {
        PreparedStatement prepare = null;

        String kodebarang = bar.getKodeBarang();
        String namabarang = bar.getNamaBarang();
        double hargabeli = bar.getHargaBeli();
        double hargajual = bar.getHargaJual();
        double jumlah = bar.getJumlah();

        String sql = "call ubahBarang (?,?,?,?,?)";
        prepare = conn.prepareStatement(sql);
        prepare.setString(1, kodebarang);
        prepare.setString(2, namabarang);
        prepare.setDouble(3, hargabeli);
        prepare.setDouble(4, hargajual);
        prepare.setDouble(5, jumlah);

        prepare.executeUpdate();
    }

    public Barang cariBarang(Barang bar) throws SQLException {
        CallableStatement cl = null;

        String sql = " call cariBarang(?,?,?,?,?) ";
        cl = conn.prepareCall(sql);
        cl.setString(1, bar.getKodeBarang());
        cl.registerOutParameter(2, java.sql.Types.VARCHAR);
        cl.registerOutParameter(3, java.sql.Types.DOUBLE);
        cl.registerOutParameter(4, java.sql.Types.DOUBLE);
        cl.registerOutParameter(5, java.sql.Types.DOUBLE);

        cl.execute();
        String namab = cl.getString(2);
        Double jual = cl.getDouble(3);
        Double beli = cl.getDouble(4);
        Double jumlh = cl.getDouble(5);

        cl.execute();

        bar.setNamaBarang(namab);
        bar.setHargaJual(jual);
        bar.setHargaBeli(beli);
        bar.setJumlah(jumlh);

        return bar;
    }

    public List<Barang> getAllBarang() throws SQLException {
        PreparedStatement statement = null;
        ResultSet result = null;

        conn.setAutoCommit(false);
        String sql = "select kodebarang, namabarang, hargabeli, hargajual, jumlah from barang order by kodebarang";
        statement = conn.prepareStatement(sql);
        result = statement.executeQuery();
        List<Barang> barangs = new ArrayList<Barang>();

        while (result.next()) {
            Barang barang = new Barang();
            String kode = result.getString("kodebarang");
            String nama = result.getString("namabarang");
            Double hargabeli = result.getDouble("hargabeli");
            Double hargajual = result.getDouble("hargajual");
            Double jumlah = result.getDouble("jumlah");
            barang.setKodeBarang(kode);
            barang.setNamaBarang(nama);
            barang.setHargaJual(hargajual);
            barang.setHargaBeli(hargabeli);
            barang.setJumlah(jumlah);
            barangs.add(barang);
        }
        conn.commit();
        return barangs;
    }
}
